package com.all.play.mysvnbanchone;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/5/15.
 * Time: 2018/5/15  9:10
 */

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder> {


    private Context context;
    private List<String> mData;
    private final LayoutInflater layoutInflater;

    public RvAdapter(Context context, List<String> mData) {
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.setData(mData,holder.getLayoutPosition());
        holder.tv.setText(mData.get(position));


        holder.itemView.setOnClickListener(new MyClickItemListener(position));

        holder.itemView.setOnLongClickListener(new MyClickLongItemListener(position));
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    public void addItem(int position) {
        mData.add(position,"jinmao"+position);
        notifyDataSetChanged();
        //notifyItemInserted(position);
    }
    public void remove(int position) {

        if (position<mData.size()){
            mData.remove(position);
            //notifyDataSetChanged();
            notifyItemRemoved(position);
        }else{
            Toast.makeText(context, "没有数据了.", Toast.LENGTH_SHORT).show();
        }

    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.tv_item);
        }


        public void setData(final List<String> mData, final int position){
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(context, ""+mData.get(position), Toast.LENGTH_SHORT).show();
//                }
//            });




        }
    }


    class MyClickItemListener implements View.OnClickListener{

        private int position;

        public MyClickItemListener(int position) {

            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
        }
    }

    class MyClickLongItemListener implements View.OnLongClickListener{

        private int position;

        public MyClickLongItemListener(int position) {

            this.position = position;
        }

        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(context, "~~~~~~~~~~~"+position, Toast.LENGTH_SHORT).show();
            return true;
        }
    }
}
